package com.controller.filter;/**
 * @AUTHOR: 我是章璐锋憨憨
 * @Data:
 * @Version:
 */

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @outhor RD
 * 2021/12/21
 */
public class LoginFilter implements Filter{

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = ((HttpServletRequest)servletRequest);
        HttpServletResponse resp = ((HttpServletResponse)servletResponse);
//        filterChain.doFilter(servletRequest,servletResponse);
//        System.out.println(req.getServletPath());
//        System.out.println("----------");
        String name = (String) req.getSession().getAttribute("LOGIN_NAME");

        if ("/login.jsp".equals(req.getServletPath()) || "/randomCode".equals(req.getServletPath()) ||
                "/login".equals(req.getServletPath()) || req.getRequestURL().toString().contains("/css") ||
                req.getRequestURL().toString().contains("/images") ||
                req.getRequestURL().toString().contains("/js11")|| req.getRequestURL().toString().contains("/calendar")){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            if (Objects.nonNull(name)){
                filterChain.doFilter(servletRequest,servletResponse);
            }else {
                resp.sendRedirect(req.getContextPath()+"/login.jsp");
            }
        }
    }
}